package
{
import com.greensock.events.LoaderEvent;
import com.greensock.loading.ImageLoader;
import com.greensock.loading.LoaderMax;
import com.greensock.loading.MP3Loader;
import com.greensock.loading.SWFLoader;
import com.greensock.loading.VideoLoader;
import com.vcommon.core.logging.LogLevel;
import com.vcommon.core.logging.Tracer;
import com.vcommon.core.logging.publishers.Output;
import com.vcommon.core.logging.publishers.SaveLogPublisher;
import com.vcommon.core.logging.publishers.TracePublisher;
import com.vcommon.core.managers.PopUpManager;
import com.vcommon.core.managers.ToolTipManager;

import fl.controls.Button;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.Security;


/**
 * author yangwz
 */		
[SWF(frameRate="24", width="760", height="600")]
public class VCommonTest extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>VCommonTest</code> instance.
	 * 
	 */
	public function VCommonTest()
	{
		if (stage) initDisplay();
		else addEventListener(Event.ADDED_TO_STAGE, initDisplay);
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	private var queue:LoaderMax;
	
	private var images:Array = ["1.png", "2.png", "3.png", "4.png", "5.png", "6.png",
	"7.png", "8.png", "9.png"];
	
	private var loader:ImageLoader;
	//==========================================================================
	//  Properties
	//==========================================================================
	//==========================================================================
	//  Methods
	//==========================================================================
	private function initDisplay(event:Event = null):void
	{
		removeEventListener(Event.ADDED_TO_STAGE, initDisplay);
		PopUpManager.getInstance().setDefaultContainer(stage);
		if (CONFIG::debugging)
		{
			//init logging
			Output.MAX_LINE = 10000;
			var output:Output = new Output(200, true);
			stage.addChild(output);
			stage.removeChild(output);
			Tracer.logger.addPublisher(new TracePublisher());
			Tracer.logger.addPublisher(new SaveLogPublisher());
			Tracer.logger.level = LogLevel.DEBUG;
			Tracer.info("sandboxType:" + Security.sandboxType);
		} 
//		queue = new LoaderMax({name:"loaderMax", 
//			requireWithRoot:root, onProgress:onProgressHandler, 
//			onComplete:onCompleteHandler, onError:onErrorHandler
//		});
//		queue.maxConnections = 100;
//		queue.append(new ImageLoader("assets/1.png",{/*container:this,*/ width:200, height:500, scaleMode:ScaleMode.PROPORTIONAL_INSIDE, x:10, y:10}));
//		queue.append(new ImageLoader("assets/2.png",{container:this, scaleMode:ScaleMode.PROPORTIONAL_INSIDE, x:300, y:10}));
//		queue.load();
		LoaderMax.activate([ImageLoader, VideoLoader, SWFLoader, MP3Loader]);
		queue = LoaderMax.parse(images, {requireWithRoot:root, onComplete:onCompleteHandler, onProgress:onProgressHandler}, {autoPlay:false});
		queue.prependURLs("assets/"); //saves us from having to include this in every URL in the array, and makes switching servers very easy.
//		queue.load();
		stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		
		loader = new ImageLoader("assets/5.png");
		loader.load();
		var button:Button = new Button();
		button.height = 200;
		button.label = "ok1515" +
			"\n" + "dsds";
		button.name = "1";
		var removeBtn:Button = new Button();
		removeBtn.name = "2";
		addChild(removeBtn);
		removeBtn.x = button.x + button.width;
		removeBtn.addEventListener(MouseEvent.CLICK, buttonClickHandler);
		addChild(button);
		button.addEventListener(MouseEvent.CLICK, buttonClickHandler);
		ToolTipManager.QUASH_TIME = 0;
		ToolTipManager.TRIGG_TIME = 0;
		ToolTipManager.getInstance().addToolTip(button, "ok");
	}
	
	private function buttonClickHandler(event:MouseEvent):void
	{
		if (event.target.name == "1")
		{
			PopUpManager.getInstance().addPopUp(loader.content);
			PopUpManager.getInstance().centerPopUp(loader.content);
		}
		else
		{
			PopUpManager.getInstance().removePopUp(loader.content);
//			PopUpManager.getInstance().centerPopUp(sprite);
		}
	}
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function onProgressHandler(event:LoaderEvent):void
	{
		Tracer.debug(int(queue.bytesLoaded/queue.bytesTotal * 100) + "%");
	}
	
	private function onCompleteHandler(event:LoaderEvent):void
	{
		for (var i:int = 1; i < 10; i++)
		{
			var image:String = "assets/" + i + ".png";
			var loader:ImageLoader = LoaderMax.getLoader(image);
			loader.content.x = Math.random() * (stage.stageWidth - loader.content.width);
			loader.content.y = Math.random() * (stage.stageHeight - loader.content.height);
//			addChild(loader.content);
		}
	}
	
	private function onErrorHandler(event:LoaderEvent):void
	{
		
	}
	
	private function mouseDownHandler(event:MouseEvent):void
	{
		trace(event.target);
		var sprite:Sprite = event.target as Sprite;
		if (sprite)
		{
			sprite.startDrag();
//			sprite.filters = [new GlowFilter(0xFF0000, 1, 12, 12)];
		}
	}
	
	private function mouseUpHandler(event:MouseEvent):void
	{
		var sprite:Sprite = event.target as Sprite;
		if (sprite)
		{
			sprite.stopDrag();
		}
	}
}
}